package com.knowledgePointsByMyTest.thread;

/**
 * @author yulongtian
 * @create 2024-11-08 16:15
 */
public class ThreadLocaTest {
    private static ThreadLocal<String> local = new ThreadLocal<String>();
    private static ThreadLocal<Integer> local_01 = new ThreadLocal<Integer>();

    public static void main(String[] args) throws InterruptedException {

        new Thread(() -> {
            ThreadLocaTest.local.set("xdclass_A");
            ThreadLocaTest.local_01.set(520);
            print("A");
            //打印本地变量
            System.out.println("清除后：" + local.get());
        }, "A").start();
        Thread.sleep(1000);

        new Thread(() -> {
            ThreadLocaTest.local.set("xdclass_B");
            print("B");
            System.out.println("清除后 " + local.get());
        }, "B").start();
    }

    static void print(String str) {
        //打印当前线程中本地内存中变量的值
        System.out.println(str + " :" + local.get());
        System.out.println(str + " :" + local_01.get());
        //清除内存中的本地变量
        local.remove();
        local_01.remove();
    }
}
